package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.domain.BuryingPointEntity;
import com.ruoyi.system.domain.VersionEntity;
import com.ruoyi.system.mapper.BuryingPointMapper;
import com.ruoyi.system.service.BuryingPointService;
import com.ruoyi.system.service.VersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
@Transactional
public class BuryingPointServiceImpl extends ServiceImpl<BuryingPointMapper, BuryingPointEntity> implements BuryingPointService {

    @Resource
    private VersionService versionService;

    @Override
    public void add(BuryingPointEntity buryingPointEntity) {
        if(buryingPointEntity.getAppVersionId()!=null){
            VersionEntity versionEntity = versionService.getById(buryingPointEntity.getAppVersionId());
            buryingPointEntity.setAppVersion(versionEntity.getVersion());
        }
        String ip = IpUtils.getIpAddr();
        buryingPointEntity.setIp(ip);
        buryingPointEntity.setAddr(AddressUtils.getRealAddressByIP(ip));
        buryingPointEntity.setCreatedDatetime(new Date());
        save(buryingPointEntity);
    }
}
